package com.ztao.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.model.common.behavior.UnLikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.user.pojos.ApUser;
import com.heima.utils.thread.AppThreadLocalUtil;
import com.ztao.common.constants.BehaviorConstants;
import com.ztao.redis.CacheService;
import com.ztao.service.ApUnlikesBehaviorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * APP不喜欢行为表 服务实现类
 */
@Service
@Slf4j
public class ApUnlikesBehaviorServiceImpl implements ApUnlikesBehaviorService {
 
    @Resource
    private CacheService cacheService;
 
    /**
     * 用户行为 - 不喜欢
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unlike(UnLikesBehaviorDto dto) {
        System.out.println("dto啊啊啊啊啊啊啊Id = " + dto.getArticleId());
        // 1. 检查参数
        if(dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
 
        // 2. 获取用户信息
        ApUser user = AppThreadLocalUtil.getUser();
        if(user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
 
        // 3. 根据操作类型进行对应操作
        if(dto.getType() .equals(0)) {
            // 不喜欢
            log.info("保存当前key: {} {} {}", dto.getArticleId(), user.getId(), dto);
            cacheService.hPut(BehaviorConstants.UN_LIKE_BEHAVIOR + dto.getArticleId().toString(), user.getId().toString(), JSON.toJSONString(dto));
        } else {
            // 取消不喜欢
            log.info("删除当前key:{} ,{}, {}", dto.getArticleId(), user.getId(), dto);
            cacheService.hDelete(BehaviorConstants.UN_LIKE_BEHAVIOR + dto.getArticleId().toString(), user.getId().toString());
        }
 
        // 4. 结果返回
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}